/*
 * =====================================================================================
 *
 *       Filename:  Goat.cpp
 *
 *    Description:  Implementation of Goat
 *
 *        Version:  1.0
 *        Created:  01/06/12 04:38:13
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  LittleQ Su (), 98703060@nccu.edu.tw
 *        Company:  National Chengchi University, The Department of Computer Science
 *
 * =====================================================================================
 */
#include "Goat.h"

Goat::Goat (World* world, int x, int y) : Creature(world, x, y) {
    this -> _id_word = GOAT;
    this -> _max_age = 70;
    this -> _gen_age[0] = 50;
    this -> _gen_age[1] = 55;
    this -> _food_point = 20;

}

void Goat::action() {
    this->move();
    this->genChild();
    _age++;
    this->detectDead();
}

void Goat::detectDead() {
    if ( _age >= _max_age ) {
        ((World*)_world) -> removeCreature(this->x, this->y);
    } else if ( _food_point <= 0 ) {
        ((World*)_world) -> removeCreature(this->x, this->y);
    }
}

void Goat::move() {
    if ( this -> _age >= this->_gen_age[0] && this -> _age <= this->_gen_age[1] ) {
        return ;
    }
    int new_x, new_y;
    do {
        new_x = this -> x + ((World*)this->_world)->getRandom(-1, 1);
        new_y = this -> y + ((World*)this->_world)->getRandom(-1, 1);
    } while (new_x == 0 && new_y==0 );
    
    if ( new_x < 0 || new_x > 34 || new_y < 0 || new_y > 19 ) {
        return;
    }

    enum CREATURE_TYPE next = ((World*)this->_world)->getCreature(new_x,new_y);

    if ( next == GOAT ) { return; }
    if ( next == GRASS ) {
        ((Goat*)this)->addFood(5);
        ((World*)_world)->removeCreature(new_x, new_y);
    }
    ((World*)_world)->moveCreature(this->x, this->y, new_x, new_y);
    this -> x = new_x;
    this -> y = new_y;
    this -> _moved = true;

    _food_point--;

}

void Goat::addFood(int food) {
    this -> _food_point += food;
}
